import React from 'react';
// import {
// 	Route,
// 	Link
// } from 'react-router-dom';
import All from './widget/All.jsx';
import Sort from './widget/Sort.jsx';
import MpUrl from '@/route/getparam';
import './index.scss';
import ajax from '@/api/public';
import { getDepartment, getCustomInfo, getIndex, getSale } from '@/api/index';
import { checkPublicLogin } from '@/api/user'

class Home extends React.Component{
	constructor (props) {
		super(props)
		this.state={
			sortTap: 1,
			sortState: 1,
			sortTime: 1,
			navList: [],
			company_id: '',
			sortData: '',
			navId: 0, // 部门id
			timeType: 1, // 数据时间选择
			contentNumber: 0,
			chartData: '', // 图表数据
			content: '',
			chartTimeType: 1, // 图表时间选择
			data: { }// 首页其他数据
		}
	}
	componentWillMount () {
		let getUrl = new MpUrl()
		// this.setState({
		// 	company_id: getUrl.get('company_id')
		// }, () => {
		// 	getDepartment({
		// 		company_id: this.state.company_id
		// 	}).then(res => {
		// 		this.setState({
		// 			navList: res.data,
		// 			navId: res.data[0].position_id
		// 		}, () => {
		// 			ajax.fetchAll([getIndex(this.getParams({type: this.state.timeType})), getCustomInfo(this.getParams({type: this.state.chartTimeType}))]).then(res => {
		// 				this.setState({
		// 					data: res[0].data,
		// 					chartData: res[1].data,
		// 					content: <All data={res[0].data} dept={this.state.navId} chartData={res[1].data} changeTime={value => this.changeTime(value)} clientTap={value => this.clientTap(value)} />
		// 				})
		// 			})
		// 		})
		// 	})
		// })
		if (getUrl.get('code')) {
			checkPublicLogin({code: getUrl.get('code'), company_id: getUrl.get('company_id'), apps_agent_id: 1000003}).then((res) => {
				if (res.code === 1) {
					this.setState({ // 获取公司id
						company_id: getUrl.get('company_id')
					}, () => {
						getDepartment({ // 获取部门列表
							company_id: this.state.company_id
						}).then(res => {
							this.setState({
								navList: res.data,
								navId: res.data[0].position_id
							}, () => {
								ajax.fetchAll([getIndex(this.getParams({type: this.state.timeType})), getCustomInfo(this.getParams({type: this.state.chartTimeType}))]).then(res => {
									this.setState({
										data: res[0].data,
										chartData: res[1].data,
										content: <All data={res[0].data} dept={this.state.navId} chartData={res[1].data} changeTime={value => this.changeTime(value)} clientTap={value => this.clientTap(value)} />
									})
								})
							})
						})
					})
				} else {
					window.location.pathname = '/webBoss/index'
				}
			})
		} else {
			if (navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1) {
				window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww0f1092726464b49d&redirect_uri=' +
        encodeURIComponent(window.location.href) +
        '&response_type=code&scope=snsapi_userinfo&agentid=1000003&state=req#wechat_redirect'
			}
		}
	}
	getParams (data) { // 参数整理
		return {
			company_id: this.state.company_id,
			dept: this.state.navId,
			...data
		}
	}
	changeParams (params) { // 修改Sort 数据
		this.setState(params, () => {
			this._getSale()
		})
	}
	changeSection (section) {
		if (this.state.contentNumber === 0) {
			this.setState({
				navId: section
			}, () => {
				ajax.fetchAll([getIndex(this.getParams({type: this.state.timeType})), getCustomInfo(this.getParams({type: this.state.chartTimeType}))]).then(res => {
					this.setState({
						data: res[0].data,
						chartData: res[1].data,
						content: <All data={res[0].data} dept={this.state.navId} chartData={this.state.chartData} changeTime={value => this.changeTime(value)} clientTap={value => this.clientTap(value)} />
					})
				}) 
			})
		} else {
			this.setState({
				navId: section
			}, () => {
				this._getSale()
			})
		}
	}
	_getSale () {
		getSale(this.getParams({type: this.state.sortTime, f_type: this.state.sortTap, m_type: this.state.sortState})).then(res => {
			this.setState({
				sortData: res.data,
				content: <Sort data={res.data} changeParams={value => this.changeParams(value)} />
			})
		})
	}
	_getCustomInfo () {
		getCustomInfo(this.getParams({type: this.state.chartTimeType})).then(res => {
			this.setState({
				chartData: res.data,
				content: <All data={this.state.data} dept={this.state.navId} chartData={res.data} changeTime={value => this.changeTime(value)} clientTap={value => this.clientTap(value)} />
			})
		})
	}
	_getIndex () {
		getIndex(this.getParams({type: this.state.timeType})).then(res => {
			this.setState({
				data: res.data,
				content: <All data={res.data} dept={this.state.navId} chartData={this.state.chartData} changeTime={value => this.changeTime(value)} clientTap={value => this.clientTap(value)} />
			})
		})
	}
	clientTap (tap) { // 改变图表时间
		this.setState({
			chartTimeType: tap
		}, () => {
			this._getCustomInfo()
		})
	}
	changeTime (timeType) { // 改变数据时间
		this.setState({
			timeType: timeType
		}, () => {
			this._getIndex()
		})
	}
	changeContent (index) { // 切换tap
		if (index === 1) {
			this.setState({
				contentNumber: index,
			}, () => {
				if (this.state.sortData === '') {
					this._getSale()
				} else {
					this.setState({
						content: <Sort data={this.state.sortData} changeParams={value => this.changeParams(value)} />
					})
				}
			})
		} else {
			this.setState({
				contentNumber: index,
				content: <All data={this.state.data} dept={this.state.navId} chartData={this.state.chartData} changeTime={value => this.changeTime(value)} />
			})
		}
	}
	render() {
		return(
			<div className='home-contain'>
				<header className='header-nav'>
					<ul>
						{
							this.state.navList.map((item, index) => {
								return(
									<li className={item.position_id === this.state.navId ? 'active' : ''} onClick={this.changeSection.bind(this, item.position_id)} key={index}>
										{item.position_name}
									</li>
								)
							})
						}
					</ul>
				</header>
				<div className='change-group'>
					<span onClick={this.changeContent.bind(this, 0)} className={!this.state.contentNumber ? 'active' : ''}>总览</span>
					<span onClick={this.changeContent.bind(this, 1)} className={!!this.state.contentNumber ? 'active' : ''}>销售排行</span>
				</div>
				<div className="home-content">
					{this.state.content}
				</div>
			</div>
		)
	}
}
export default Home;
